import { ShaohuoFranchiseeService, ShaohuoPriceService } from 'services/Api'
import { mapState } from 'vuex'

/* 状态相关（恢复禁用按钮）
-------------------------- */
export const franchiseeStatusMixin = {
  data () {
    return {
      isStatusOpposite: true
    }
  },
  computed: {
    // 捎货类型 禁用,解禁参数
    alterFranchiserStatusParam () {
      return {
        acc_id: this.acc_id,
        ids: this.selectIds,
        status: this.status
      }
    }
  },
  methods: {
    // 改变捎货类型状态接口
    currentStatusApi () {
      return ShaohuoFranchiseeService.alterFranchiserStatus(this.alterFranchiserStatusParam)
    }
  }
}

/* 编辑相关
-------------------------- */
export const franchiseeEditMixin = {
  data () {
    return {
      editMetaData: null,
      chooseProvinceId: ''
    }
  },
  computed: {
    ...mapState([ 'acc_id' ]),
    // 获取省列表参数
    getAllProvinceParam () { return { acc_id: this.acc_id } },
    // 获取城市列表参数
    getCityByIdParam () {
      return {
        acc_id: this.acc_id,
        id: this.chooseProvinceId
      }
    }
  },
  methods: {
    // 需要获取省，市数据，重写getData方法
    getData (data) {
      this.editMetaData = data.row
      const index = data.$index
      this.AsyncLoading('修改', index, this.getEditDataApi)
    },
    async getEditDataApi () {
      const data = { ...this.editMetaData }
      const citys = data.regions.map(region => this.getCityByIdApi(region.provinceid))
      const promises = [ this.getAllProvinceApi(), ...citys ]
      const res = await Promise.all(promises)
      const province = res.shift()
      data.regions.forEach((region, index) => {
        region.provinces = province
        region.cities = res[index]
      })
      this.editData = data
      console.log(data)
      this._showDialog()
    },
    // 获取省份列表接口
    async getAllProvinceApi () {
      const [ err, res ] = await ShaohuoPriceService.getAllProvince(this.getAllProvinceParam)
      if (err) return this.$message.error(err.message || '获取失败')
      return res
    },
    // 获取城市列表接口
    async getCityByIdApi (id) {
      this.chooseProvinceId = id
      const [ err, res ] = await ShaohuoPriceService.getCityAreaById(this.getCityByIdParam)
      if (err) return this.$message.error(err.message || '获取失败')
      return res
    }
  }
}
